﻿using DapperExtensions;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;

namespace Hangjing.SQLServerDAL
{
    public class BaseDal<T> : IBaseDal<T> where T : class, new()
    {
        public T GetEntity(Func<T, bool> exp)
        {
            throw new Exception();
        }

        public int Insert(T entity)
        {
            using (SqlConnection cn = new SqlConnection(DBConfig.UserCenterDBKey))
            {
                cn.Open();

                var model = cn.Insert<T>(entity);

                cn.Close();

                return model;
            }
        }

        public bool Update(T entity)
        {
            bool result = false;
            using (SqlConnection cn = new SqlConnection(DBConfig.UserCenterDBKey))
            {
                cn.Open();
                try
                {
                    result = cn.Update<T>(entity);
                }
                catch (Exception ex)
                {
                    result = false;
                    throw ex;
                }
                finally
                {
                    cn.Close();
                }
                return result;
            }
        }

        public bool Delete(T entity)
        {
            bool result = false;
            using (SqlConnection cn = new SqlConnection(DBConfig.UserCenterDBKey))
            {
                cn.Open();
                try
                {
                    result = cn.Delete(entity);
                }
                catch (Exception ex)
                {
                    result = false;
                    throw ex;
                }
                finally
                {
                    cn.Close();
                }
            }
            return result;
        }

        /// <summary>
        /// 批量添加实体
        /// </summary>
        /// <param name="entityList">实体集合</param>
        /// <returns></returns>
        public bool Insert(List<T> entityList)
        {
            bool result = false;
            using (SqlConnection cn = new SqlConnection(DBConfig.UserCenterDBKey))
            {
                cn.Open();
                try
                {
                    cn.Insert<T>(entityList);
                    result = true;
                }
                catch (Exception ex)
                {
                    result = false;
                    throw ex;
                }
                finally
                {
                    cn.Close();
                }
            }
            return result;
        }

        public T GetEntity(object id)
        {
            using (SqlConnection cn = new SqlConnection(DBConfig.UserCenterDBKey))
            {
                cn.Open();

                var model = cn.Get<T>(id);

                cn.Close();

                return model;
            }
        }

        #region 排序参数转换
        /// <summary>
        /// 转换排序参数
        /// </summary>
        /// <param name="sortlist">转换排序参数集合</param>
        /// <param name="defaultSort">默认排序参数</param>
        /// <returns>List<ISort></returns>
        public List<ISort> ConvertSortList(List<Model.SortParameterInfo> sortlist, ISort defaultSort)
        {
            List<ISort> stlist = new List<ISort>();
            if (sortlist != null)
            {
                sortlist.ForEach(m =>
                {
                    ISort st = ConvertSort(m);
                    stlist.Add(st);
                });
            }
            else
            {
                stlist.Add(defaultSort);
            }
            return stlist;
        }
        /// <summary>
        /// 将SortParameterInfo转为ISort
        /// </summary>
        /// <param name="sort">SortParameterInfo对象</param>
        /// <returns>ISort</returns>
        public ISort ConvertSort(Model.SortParameterInfo sort)
        {
            ISort st = new Sort();
            if (sort != null)
            {
                st.Ascending = sort.Ascending == Model.PagerSortType.ASC;
                st.PropertyName = sort.PropertyName;
            }
            return st;
        }
        #endregion


        public SqlConnection DBSession { get; private set; }

        public void SetDBSession(SqlConnection dbSession)
        {
            DBSession = dbSession;
        }

        #region 分页
        /// <summary>
        /// 分页
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="allRowsCount"></param>
        /// <param name="predicate"></param>
        /// <param name="sort"></param>
        /// <param name="buffered"></param>
        /// <returns></returns>
        public IEnumerable<T> GetPageList<T>(int pageIndex, int pageSize, out long allRowsCount,
        object predicate = null, IList<ISort> sort = null, bool buffered = true) where T : class
        {
            if (sort == null)
            {
                sort = new List<ISort>();
            }
            IEnumerable<T> entityList = DBSession.GetPage<T>(predicate, sort, pageIndex, pageSize, null, null, buffered);
            allRowsCount = DBSession.Count<T>(predicate);
            return entityList;
        }

        /// <summary>
        /// 统计记录总数
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="predicate"></param>
        /// <param name="buffered"></param>
        /// <returns></returns>
        //public int Count<T>(object predicate, bool buffered = false) where T : class
        //{
        //    return DBSession.Count<T>(predicate);
        //}
        #endregion
    }
}
